package system

import (
	"context"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
)

type SysDrugsService struct{}

// CreateSysDrugs 创建药品信息记录
// Author [yourname](https://github.com/yourname)
func (drugsService *SysDrugsService) CreateSysDrugs(ctx context.Context, drugs *system.SysDrugs) (err error) {
	err = global.GVA_DB.Create(drugs).Error
	return err
}

// DeleteSysDrugs 删除药品信息记录
// Author [yourname](https://github.com/yourname)
func (drugsService *SysDrugsService) DeleteSysDrugs(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&system.SysDrugs{}, "id = ?", ID).Error
	return err
}

// DeleteSysDrugsByIds 批量删除药品信息记录
// Author [yourname](https://github.com/yourname)
func (drugsService *SysDrugsService) DeleteSysDrugsByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]system.SysDrugs{}, "id in ?", IDs).Error
	return err
}

// UpdateSysDrugs 更新药品信息记录
// Author [yourname](https://github.com/yourname)
func (drugsService *SysDrugsService) UpdateSysDrugs(ctx context.Context, drugs system.SysDrugs) (err error) {
	err = global.GVA_DB.Model(&system.SysDrugs{}).Where("id = ?", drugs.Id).Updates(&drugs).Error
	return err
}

// GetSysDrugs 根据ID获取药品信息记录
// Author [yourname](https://github.com/yourname)
func (drugsService *SysDrugsService) GetSysDrugs(ctx context.Context, ID string) (drugs system.SysDrugs, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&drugs).Error
	return
}

// GetSysDrugsInfoList 分页获取药品信息记录
// Author [yourname](https://github.com/yourname)
func (drugsService *SysDrugsService) GetSysDrugsInfoList(ctx context.Context, info systemReq.SysDrugsSearch, store_id uint) (list []system.SysDrugs, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system.SysDrugs{})
	var drugss []system.SysDrugs
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	// 多字段 LIKE 模糊查询（name、code、guige、address 中任意一个包含关键词）
	if info.Keyword != "" {
		keyword := "%" + info.Keyword + "%"
		db = db.Where("name LIKE ? OR code LIKE ? OR guige LIKE ? OR address LIKE ?", keyword, keyword, keyword, keyword)
	}

	db = db.Where("store_id = ?", store_id)
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&drugss).Error
	return drugss, total, err
}
func (drugsService *SysDrugsService) GetSysDrugsPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}

// GetDrugsInfoList 分页获取药品信息记录
// Author [yourname](https://github.com/yourname)
func (drugsService *SysDrugsService) GetDrugsInfoList(ctx context.Context, info systemReq.SysDrugsSearch, store_id uint) (list []system.SysDrugs, total int64, err error) {

	// 创建db
	db := global.GVA_DB.Model(&system.SysDrugs{})
	var drugss []system.SysDrugs
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}
	db = db.Where("store_id = ?", store_id)

	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Find(&drugss).Error
	return drugss, total, err
}
